from rest_framework import serializers
from decimal import Decimal

from equity_admin.filters_activities import *
from equity_mall.utils.utils import get_level_list
from users.models import *


class ChannelModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = Channel
        fields = ['id', 'mchid', 'name']


class ActivityListSerializer(serializers.ModelSerializer):

    available_area = serializers.SerializerMethodField(read_only=True)
    available_begin_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    available_end_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    remain_point = serializers.SerializerMethodField(read_only=True)
    progress = serializers.SerializerMethodField(read_only=True)
    activity_state_str = serializers.CharField(source='get_activity_state_display', read_only=True)

    class Meta:
        model = Activity
        fields = ('id', 'name', 'available_area', 'available_begin_time', 'available_end_time', 'max_point', 'distributed_point', 'remain_point', 'progress', 'activity_state',
                  'activity_state_str')

    def get_available_area(self, obj):
        try:
            level = obj.level
            return f'{level.prov_name}{level.city_name}{level.district_name}{level.branch_name}'.replace('None', '')
        except:
            return ''

    def get_remain_point(self, obj):
        try:
            return obj.max_point - obj.distributed_point
        except:
            return '0'

    def get_progress(self, obj):
        try:
            return f'{(obj.distributed_point / obj.max_point * 100).quantize(Decimal("0.00"))}%'
        except:
            return '0%'


class ActivityDataListModelSerializer(serializers.ModelSerializer):

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    received_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    activity_name = serializers.SerializerMethodField(read_only=True)
    state_str = serializers.CharField(source='get_state_display', read_only=True)
    companyuser_nickname = serializers.SerializerMethodField(read_only=True)
    level_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = ActivityData
        fields = ('id', 'create_time', 'activity_name', 'point', 'state', 'state_str', 'received_time', 'companyuser_nickname', 'level_name')

    def get_activity_name(self, obj):
        try:
            return obj.activity.name
        except:
            return ''

    def get_companyuser_nickname(self, obj):
        try:
            return obj.companyuser.nickname
        except:
            return ''

    def get_level_name(self, obj):
        try:
            return obj.companyuser.level.name
        except:
            return ''


class ActivityDataRetrieveModelSerializer(serializers.ModelSerializer):

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    received_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)
    activity_name = serializers.SerializerMethodField(read_only=True)
    state_str = serializers.CharField(source='get_state_display', read_only=True)
    companyuser_nickname = serializers.SerializerMethodField(read_only=True)
    companyuser_level = serializers.SerializerMethodField(read_only=True)
    customeruser_realname = serializers.SerializerMethodField(read_only=True)
    customeruser_level = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = ActivityData
        fields = ('id', 'create_time', 'activity_name', 'point', 'state', 'state_str', 'received_time', 'companyuser_nickname', 'companyuser_level',
                  'customeruser_realname', 'customeruser_level')

    def get_activity_name(self, obj):
        try:
            return obj.activity.name
        except:
            return ''

    def get_companyuser_nickname(self, obj):
        try:
            return obj.companyuser.nickname
        except:
            return ''

    def get_companyuser_level(self, obj):
        try:
            level = obj.companyuser.level
            return f'{level.prov_name}-{level.city_name}-{level.district_name}-{level.branch_name}'.replace('-None', '')
        except:
            return ''

    def get_customeruser_realname(self, obj):
        try:
            return obj.customer_user.realname
        except:
            return ''

    def get_customeruser_level(self, obj):
        try:
            level = obj.customer_user.level
            return f'{level.prov_name}-{level.city_name}-{level.district_name}-{level.branch_name}'.replace('-None', '')
        except:
            return ''